package com.rockwell.sniffyhunter;

import com.rockwell.sniffyhunter.R;
import com.rockwell.sniffyhunter.model.MazePath;
import com.rockwell.sniffyhunter.views.PathView;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

public class MazeActivity extends Activity {
	public static final String KEY_PATH_STRING = "path_string";
	public static final String KEY_INITIAL_X = "initial_x";
	public static final String KEY_INITIAL_Y = "initial_y";
	public static final String KEY_INITIAL_DIR = "initial_dir";
	public static final String KEY_INITIAL_MAZENO = "initial_mazeno";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_maze);
		
		//Intent intent = getIntent();
		//m_pathString = intent.getExtras().getString(KEY_PATH_STRING);
		
		Spinner mazeno = (Spinner)findViewById(R.id.spinner_mazeno);  
		mazeno.setSelection(SettingsActivity.getInitialMazeNo(this).ordinal());
		mazeno.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					 int position, long id) {
				SettingsActivity.setInitialMazeNo(MazeActivity.this, MazePath.MazeNo.values()[position]);
				drawPath();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		Spinner x = (Spinner)findViewById(R.id.spinner_x);  
		x.setSelection(Math.min(SettingsActivity.getInitialX(this), SettingsActivity.getMazeWidth(this)-1));
		x.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					 int position, long id) {
				SettingsActivity.setInitialX(MazeActivity.this, position);
				drawPath();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		Spinner y = (Spinner)findViewById(R.id.spinner_y);  
		y.setSelection(Math.min(SettingsActivity.getInitialY(this), SettingsActivity.getMazeHeight(this)-1));
		y.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					 int position, long id) {
				SettingsActivity.setInitialY(MazeActivity.this, position);
				drawPath();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		Spinner dir = (Spinner)findViewById(R.id.spinner_dir);
		dir.setSelection(SettingsActivity.getInitialDir(this).ordinal());
		dir.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					 int position, long id) {
				SettingsActivity.setInitialDir(MazeActivity.this, MazePath.Direction.values()[position]);
				drawPath();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});

		PathView pv = (PathView)findViewById(R.id.path_view);
		pv.setMazeWidth(SettingsActivity.getMazeWidth(this));
		pv.setMazeHeight(SettingsActivity.getMazeHeight(this));
	}

	/*
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.maze, menu);
		return true;
	}
	 */
	
	@Override
	public void onResume() {
		super.onResume();

		drawPath();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		drawPath();
	}
	
	public void onStartPath(View v) {
		startPathActivity();
	}
	
	public void onDrawPath(View v) {
		drawPath();
	}
	
	private void startPathActivity() {
		Intent intent = new Intent(this, PathActivity.class);
		Spinner mazeNo = (Spinner)findViewById(R.id.spinner_mazeno);
		String strSelected =  mazeNo.getSelectedItem().toString();
		intent.putExtra("mazeNo", strSelected);
		startActivityForResult(intent, 1);
	}
		
	private void drawPath() {
		String path = MazePath.loadPredefinedPath(this);
		if (path.length() > 3) {
			Spinner x = (Spinner)findViewById(R.id.spinner_x);  
			int initialX = x.getSelectedItemPosition();
			
			Spinner y = (Spinner)findViewById(R.id.spinner_y);  
			int initialY = y.getSelectedItemPosition();
			
			Spinner dir = (Spinner)findViewById(R.id.spinner_dir);
			MazePath.Direction initialDir = MazePath.Direction.valueOf(dir.getSelectedItem().toString());
			
			PathView pv = (PathView)findViewById(R.id.path_view);
			pv.draw(path, initialX, initialY, initialDir);
		}
	}
}
